
do "E:/ReplicateBuild/02_code/00_environment/00_set_environment.do"


use "$basedata/mb_clean.dta", clear

* keep only the standard tests
replace score_ma = . if !inlist(ma_test_id,"RG","MA03","MA04","MA05","MA06","MA07","MA08")
* keep only FOCAL
keep if lea=="XXX" // hide identity
gen  app_year=year-1
gen ncerdc_schlcode=real( schlcode) 
collapse score_ma, by(ncerdc_schlcode app_year)
ren score_ma mn_achievement
merge 1:m ncerdc_schlcode app_year using "$basedata/estimationdata/All_T_J/All_T_J_schoolvar_buff_0_0_lf"
drop if _m==1
drop _m

foreach suff2 in "" "_race" "_ach" {
gen mean_n_ma`suff2' = mean_n1_ma`suff2' + mean_n2_ma`suff2'
}

keep jtidx frac_black frac_hisp app_year titleI school_exceeded school_met p_school_m1_ma* p_school_m2_ma* mn_achievement mean_n_ma* mean_n1_ma* mean_n2_ma*
sort jtidx 

tempfile schooldata
save "`schooldata'", replace

use "$basedata/estimationdata/All_T_J/All_T_J_teachvar_buff_0_0_lf", clear


egen type1_ma_pre=std(mu_jt_m1_hat_preY_ma)
egen type2_ma_pre=std(mu_jt_m2_hat_preY_ma)

/* split on each dimension separately */

egen type1_high_pre=cut(type1_ma_pre), group(2)
egen type2_high_pre=cut(type2_ma_pre), group(2)


cap gen poor_adv= type2_ma_pre-type1_ma_pre


gen exp2=tchr_exp_pay_level==2 | tchr_exp_pay_level==3
	replace exp2=0 if tchr_exp_pay_level==.
gen exp4=tchr_exp_pay_level>=4 & tchr_exp_pay_level<=6
	replace exp4=0 if tchr_exp_pay_level==.
gen exp6=tchr_exp_pay_level>6
	replace exp6=0 if tchr_exp_pay_level==.
gen exp_missing=tchr_exp_pay_level==.

gen evaas_missing = evaas_score==.
replace evaas_score = 0 if evaas_missing==1

keep itidx female poor_adv black hispanic mu_jt_preY_mean_school_ma* type1_ma type2_ma  ncerdc_exp exp2 exp4 exp6 exp_missing mu_jt_m1_hat_preY_ma* mu_jt_m2_hat_preY_ma*  mu_jt_preY_mean_school_ma* evaas_missing evaas_score  p_school_m*_all
sort itidx 

tempfile teachdata
save  "`teachdata'", replace


use "$basedata/estimationdata/All_T_J/All_T_J_choices_buff_0_0_lf", clear

merge m:1 itidx using `teachdata'
drop  _merge 
sort jtidx 
merge m:1 jtidx using  `schooldata'

drop _m

cap drop sortvar
gen sortvar = _n
sort itidx
merge m:1 itidx using "$temp/VAimputed", keepusing(VA1_imputed_0samp VA2_imputed_0samp)
assert _m==3
sort sortvar
drop _m sortvar

gen mu_jt_m1_hat_preY_ma_imp = mu_jt_m1_hat_preY_ma
gen mu_jt_m2_hat_preY_ma_imp = mu_jt_m2_hat_preY_ma
replace mu_jt_m1_hat_preY_ma_imp = VA1_imputed_0samp if has_va_ma_preY==0
replace mu_jt_m2_hat_preY_ma_imp = VA2_imputed_0samp if has_va_ma_preY==0

gen has_va_ma_preY_imp = mu_jt_m1_hat_preY_ma_imp!=. & mu_jt_m2_hat_preY_ma_imp!=.

gen va_ma_preY_imp = mu_jt_m1_hat_preY_ma_imp*p_school_m1_ma + mu_jt_m2_hat_preY_ma_imp*p_school_m2_ma if has_va_ma_preY_imp==1

gen	mu_jt_preY_mean_school_ma_imp = mu_jt_m1_hat_preY_ma_imp*p_school_m1_ma_all + mu_jt_m2_hat_preY_ma_imp*(1-p_school_m1_ma_all) if has_va_ma_preY_imp==1

gen imputedteacher = has_va_ma_preY==0


foreach suff2 in "" "_race" "_ach" "_cfr" "_homog" "_sM" "_imp" {
gen va_ma_dev`suff2' = va_ma_preY`suff2'- mu_jt_preY_mean_school_ma`suff2' 
}

gen commute_trunc=min(commute_time,40)
gen commute_time_miss=(commute_time==.)

replace commute_time=0 if commute_time==.

replace mn_achievement = mean_n2_ma_ach / (mean_n1_ma_ach + mean_n2_ma_ach)

foreach suff3 in "_cfr" "_homog" "_sM" "_imp" {
	gen mean_n1_ma`suff3' = mean_n1_ma
	gen mean_n2_ma`suff3' = mean_n2_ma
	gen mean_n_ma`suff3' = mean_n_ma
}

gen frac_disadv=p_school_m2_ma
gen n_disadv=mean_n2_ma

foreach suff2 in "" "_race" "_ach" "_cfr" "_homog" "_sM" "_imp" {
foreach i in mn_achievement frac_black frac_hisp n_disadv {
    gen VA_x_`i'`suff2'=mu_jt_preY_mean_school_ma`suff2'*`i'
}
}

gen blackXfrac_black=black*frac_black
gen hispanicXfrac_hisp=hispanic*frac_hisp

foreach suff2 in "" "_race" "_ach" "_cfr" "_homog" "_sM" "_imp" {
gen has_va_ma_pre`suff2'=(va_ma_preY`suff2'~=.)
gen va_ma_miss`suff2'=1-has_va_ma_pre`suff2'

bys app_year: egen mean_n1_ma_district`suff2' = mean(mean_n1_ma`suff2')
bys app_year: egen mean_n2_ma_district`suff2' = mean(mean_n2_ma`suff2')

gen mu_jt_m1_hat_preY_maN`suff2' = mu_jt_m1_hat_preY_ma`suff2' * mean_n1_ma_district`suff2'
gen mu_jt_m2_hat_preY_maN`suff2' = mu_jt_m2_hat_preY_ma`suff2' * mean_n2_ma_district`suff2'

gen va_ma_tot`suff2' = (mean_n1_ma`suff2')*mu_jt_m1_hat_preY_ma`suff2' + (mean_n2_ma`suff2')*mu_jt_m2_hat_preY_ma`suff2'

gen mu_jt_mean_school_ma`suff2' = mu_jt_m1_hat_preY_ma`suff2' * mean_n1_ma_district`suff2'/(mean_n1_ma_district`suff2'+mean_n2_ma_district`suff2') + ///
	 mu_jt_m2_hat_preY_ma`suff2' * mean_n2_ma_district`suff2'/(mean_n1_ma_district`suff2'+mean_n2_ma_district`suff2')
}

foreach suff3 in "_C" "_cfrC" {
	if "`suff3'" == "_C" {
		local suff2 = ""
	}
	if "`suff3'" == "_cfrC" {
		local suff2 = "_cfr"
	}
	gen mean_n1_ma`suff3' = (mean_n1_ma`suff2'/mean_n_ma`suff2')*(mean_n1_ma_district`suff2' + mean_n2_ma_district`suff2')
	gen mean_n2_ma`suff3' = (mean_n2_ma`suff2'/mean_n_ma`suff2')*(mean_n1_ma_district`suff2' + mean_n2_ma_district`suff2')
	gen mean_n_ma`suff3' = mean_n1_ma`suff3' + mean_n2_ma`suff3'
	
	gen va_ma_tot`suff3' = (mean_n1_ma`suff3')*mu_jt_m1_hat_preY_ma`suff2' + (mean_n2_ma`suff3')*mu_jt_m2_hat_preY_ma`suff2'
}

* choice set with buffer
gen choiceset7 = choiceset
replace choiceset7 = 1 if (PostingDate > last_person_app & PostingDate < last_person_app + 7)


foreach suff2 in "" "_race" "_ach" "_cfr" "_homog" "_sM" "_imp" {
    
local covarsch = "frac_black frac_hisp mn_achievement mean_n_ma`suff2' n_disadv"
local covartchsch= "blackXfrac_black hispanicXfrac_hisp VA_x_frac_hisp`suff2' VA_x_frac_black`suff2' VA_x_mn_achievement`suff2' VA_x_n_disadv`suff2' commute_time commute_time_miss"
local covartch = "current_lea black hispanic female mu_jt_m1_hat_preY_ma`suff2' mu_jt_m2_hat_preY_ma`suff2' exp2 exp4 exp6 exp_missing"
local covva = "va_ma_tot`suff2' va_ma_preY`suff2'"

cap gen estsamp`suff2'=choiceset
cap gen estsampall`suff2'=1
cap gen estsamp7`suff2' = choiceset7

* update sample variable for missing values of covariates
foreach i in `covarsch' `covartchsch' `covartch' `covva' {
    replace estsamp`suff2'=0 if `i'==.
	replace estsampall`suff2'=0 if `i'==.
	replace estsamp7`suff2'=0 if `i'==.
} 

* chamberlain-mundlak device
gsort itidx -estsamp`suff2'
foreach i in `covva' `covarsch' `covartchsch' {
    
	
capture confirm variable `i'_bar
if _rc {
  cap 	egen `i'_tbar=mean(`i') if estsamp`suff2'==1, by(itidx)
replace `i'_tbar = `i'_tbar[_n-1] if missing(`i'_tbar) & itidx==itidx[_n-1]  
}


}




* chamberlain-mundlak device, with buffer
gsort itidx -estsamp7`suff2'
foreach i in `covva' `covarsch' `covartchsch' {
    
	
capture confirm variable `i'_bar
if _rc {
  cap 	egen `i'_tbar7=mean(`i') if estsamp7`suff2'==1, by(itidx)
replace `i'_tbar7 = `i'_tbar7[_n-1] if missing(`i'_tbar7) & itidx==itidx[_n-1]  
}


}
}

foreach suff3 in "_C" "_cfrC" {
	
	if "`suff3'" == "_C" {
		local suff2 = ""
	}
	if "`suff3'" == "_cfrC" {
		local suff2 = "_cfr"
	}
    
local covarsch = "mean_n_ma`suff3'"
local covva = "va_ma_tot`suff3'"

gen estsamp`suff3' = estsamp`suff2'
gen estsampall`suff3' = estsampall`suff2'

* chamberlain-mundlak device
gsort itidx -estsamp`suff2'
foreach i in `covva' `covarsch' {
    
	
capture confirm variable `i'_bar
if _rc {
  cap 	egen `i'_tbar=mean(`i') if estsamp`suff2'==1, by(itidx)
replace `i'_tbar = `i'_tbar[_n-1] if missing(`i'_tbar) & itidx==itidx[_n-1]  
}

}
}


bys itidx: egen any_current = max(current_school)
bys itidx: egen any_next = max(next_school)
gen stayer = current_school==1 & next_school==1
gen attriter = current_school==1 & any_next==0

bys itidx: egen teach_stayer = max(stayer)
bys itidx: egen teach_attriter = max(attriter)

sort itidx jtidx

save "$basedata/estimationdata/All_T_J/teach_pref_est_data.dta", replace




foreach suff2 in "" "_race" "_ach" "_cfr" "_homog" "_sM" "_imp" {
	
	

use "$basedata/estimationdata/All_T_J/teach_pref_est_data.dta", clear

keep if estsampall`suff2'==1

	local extravar = ""
	if "`suff2'"=="_imp" {
		local extravar = "imputedteacher"
		keep if app_year==2015
	}

set seed 332899
gen sortvar = runiform()
gsort itidx -current_school sortvar
gen current_school_first = current_school==1 & itidx[_n-1]!=itidx

sort itidx jtidx

foreach var in itidx jtidx iidx jidx {
    ren `var' `var'_orig
	egen `var' = group(`var'_orig)
	drop `var'_orig
}



preserve

keep itidx jtidx applied interview choiceset iidx jidx dist teachidx schoolidx app_year choicesetday1 yitidx yjtidx commute_time commute_time_miss ///
 hired offer_declined withdrew positive_assessment negative_assessment va_ma_preY`suff2' mu_jt_preY_mean_school_ma`suff2' mu_jt_m1_hat_preY_maN`suff2' mu_jt_m2_hat_preY_maN`suff2' mean_n_ma`suff2' va_ma_tot`suff2' va_ma_tot`suff2'_tbar commute_time_tbar commute_time_miss_tbar ///
 frac_black frac_hisp n_disadv mn_achievement mean_n_ma`suff2' blackXfrac_black hispanicXfrac_hisp VA_x_n_disadv`suff2' VA_x_frac_hisp`suff2' VA_x_frac_black`suff2' VA_x_mn_achievement`suff2' ///
 frac_black_tbar frac_hisp_tbar mn_achievement_tbar n_disadv_tbar blackXfrac_black_tbar hispanicXfrac_hisp_tbar VA_x_n_disadv`suff2'_tbar ///
 VA_x_frac_hisp`suff2'_tbar VA_x_frac_black`suff2'_tbar VA_x_mn_achievement`suff2'_tbar mean_n_ma`suff2'_tbar current_lea black hispanic female exp2 exp4 exp6 exp_missing current_school current_school_first ///
 mu_jt_m1_hat_preY_ma`suff2' mu_jt_m2_hat_preY_ma`suff2' teach_stayer teach_attriter `extravar'
order itidx jtidx applied interview choiceset iidx jidx dist teachidx schoolidx app_year choicesetday1 yitidx yjtidx commute_time commute_time_miss ///
 hired offer_declined withdrew positive_assessment negative_assessment va_ma_preY`suff2' mu_jt_preY_mean_school_ma`suff2' mu_jt_m1_hat_preY_maN`suff2' mu_jt_m2_hat_preY_maN`suff2' mean_n_ma`suff2' va_ma_tot`suff2' va_ma_tot`suff2'_tbar commute_time_tbar commute_time_miss_tbar ///
 frac_black frac_hisp n_disadv mn_achievement mean_n_ma`suff2' blackXfrac_black hispanicXfrac_hisp VA_x_n_disadv`suff2' VA_x_frac_hisp`suff2' VA_x_frac_black`suff2' VA_x_mn_achievement`suff2' ///
 frac_black_tbar frac_hisp_tbar mn_achievement_tbar n_disadv_tbar blackXfrac_black_tbar hispanicXfrac_hisp_tbar VA_x_n_disadv`suff2'_tbar ///
 VA_x_frac_hisp`suff2'_tbar VA_x_frac_black`suff2'_tbar VA_x_mn_achievement`suff2'_tbar mean_n_ma`suff2'_tbar current_lea black hispanic female exp2 exp4 exp6 exp_missing current_school current_school_first ///
  mu_jt_m1_hat_preY_ma`suff2' mu_jt_m2_hat_preY_ma`suff2' teach_stayer teach_attriter `extravar'

outsheet using "$basedata/estimationdata/All_T_J/All_T_J_choices_buff_0_0_lf_tbar_Ndis`suff2'.csv", comma replace

restore

if "`suff2'"=="_race" | "`suff2'"=="_ach" {
	preserve
	keep itidx jtidx  mean_n1_ma`suff2' mean_n2_ma`suff2'
	order itidx jtidx  mean_n1_ma`suff2' mean_n2_ma`suff2'
	outsheet using "$basedata/estimationdata/All_T_J/All_T_J_choices_buff_0_0_lf_tbar_Ntype`suff2'.csv", comma replace
	restore
}


}

foreach suff3 in "_C" "_cfrC" {
	
	if "`suff3'" == "_C" {
		local suff2 = ""
	}
	if "`suff3'" == "_cfrC" {
		local suff2 = "_cfr"
	}
	

use "$basedata/estimationdata/All_T_J/teach_pref_est_data.dta", clear

keep if estsampall`suff3'==1

set seed 332899
gen sortvar = runiform()
gsort itidx -current_school sortvar
gen current_school_first = current_school==1 & itidx[_n-1]!=itidx

sort itidx jtidx

foreach var in itidx jtidx iidx jidx {
    ren `var' `var'_orig
	egen `var' = group(`var'_orig)
	drop `var'_orig
}



preserve

keep itidx jtidx applied interview choiceset iidx jidx dist teachidx schoolidx app_year choicesetday1 yitidx yjtidx commute_time commute_time_miss ///
 hired offer_declined withdrew positive_assessment negative_assessment va_ma_preY`suff2' mu_jt_preY_mean_school_ma`suff2' mu_jt_m1_hat_preY_maN`suff2' mu_jt_m2_hat_preY_maN`suff2' mean_n_ma`suff3' va_ma_tot`suff3' va_ma_tot`suff3'_tbar commute_time_tbar commute_time_miss_tbar ///
 frac_black frac_hisp n_disadv mn_achievement mean_n_ma`suff3' blackXfrac_black hispanicXfrac_hisp VA_x_n_disadv`suff2' VA_x_frac_hisp`suff2' VA_x_frac_black`suff2' VA_x_mn_achievement`suff2' ///
 frac_black_tbar frac_hisp_tbar mn_achievement_tbar n_disadv_tbar blackXfrac_black_tbar hispanicXfrac_hisp_tbar VA_x_n_disadv`suff2'_tbar ///
 VA_x_frac_hisp`suff2'_tbar VA_x_frac_black`suff2'_tbar VA_x_mn_achievement`suff2'_tbar mean_n_ma`suff3'_tbar current_lea black hispanic female exp2 exp4 exp6 exp_missing current_school current_school_first ///
 mu_jt_m1_hat_preY_ma`suff2' mu_jt_m2_hat_preY_ma`suff2' teach_stayer teach_attriter
order itidx jtidx applied interview choiceset iidx jidx dist teachidx schoolidx app_year choicesetday1 yitidx yjtidx commute_time commute_time_miss ///
 hired offer_declined withdrew positive_assessment negative_assessment va_ma_preY`suff2' mu_jt_preY_mean_school_ma`suff2' mu_jt_m1_hat_preY_maN`suff2' mu_jt_m2_hat_preY_maN`suff2' mean_n_ma`suff3' va_ma_tot`suff3' va_ma_tot`suff3'_tbar commute_time_tbar commute_time_miss_tbar ///
 frac_black frac_hisp n_disadv mn_achievement mean_n_ma`suff3' blackXfrac_black hispanicXfrac_hisp VA_x_n_disadv`suff2' VA_x_frac_hisp`suff2' VA_x_frac_black`suff2' VA_x_mn_achievement`suff2' ///
 frac_black_tbar frac_hisp_tbar mn_achievement_tbar n_disadv_tbar blackXfrac_black_tbar hispanicXfrac_hisp_tbar VA_x_n_disadv`suff2'_tbar ///
 VA_x_frac_hisp`suff2'_tbar VA_x_frac_black`suff2'_tbar VA_x_mn_achievement`suff2'_tbar mean_n_ma`suff3'_tbar current_lea black hispanic female exp2 exp4 exp6 exp_missing current_school current_school_first ///
  mu_jt_m1_hat_preY_ma`suff2' mu_jt_m2_hat_preY_ma`suff2' teach_stayer teach_attriter

outsheet using "$basedata/estimationdata/All_T_J/All_T_J_choices_buff_0_0_lf_tbar_Ndis`suff3'.csv", comma replace

restore

	preserve
	keep itidx jtidx  mean_n1_ma`suff3' mean_n2_ma`suff3'
	order itidx jtidx  mean_n1_ma`suff3' mean_n2_ma`suff3'
	outsheet using "$basedata/estimationdata/All_T_J/All_T_J_choices_buff_0_0_lf_tbar_Ntype`suff3'.csv", comma replace
	restore
}

local suff2 = ""
use "$basedata/estimationdata/All_T_J/teach_pref_est_data.dta", clear


keep if estsampall==1

bys itidx: egen num_apps = sum(applied)
keep if num_apps>1
drop num_apps 

sort itidx jtidx

set seed 332899
gen sortvar = runiform()
gsort itidx -current_school sortvar
gen current_school_first = current_school==1 & itidx[_n-1]!=itidx

sort itidx jtidx

foreach var in itidx jtidx iidx jidx {
    ren `var' `var'_orig
	egen `var' = group(`var'_orig)
	drop `var'_orig
}

preserve
keep itidx jtidx applied interview choiceset iidx jidx dist teachidx schoolidx app_year choicesetday1 yitidx yjtidx commute_time commute_time_miss ///
 hired offer_declined withdrew positive_assessment negative_assessment va_ma_preY`suff2' mu_jt_preY_mean_school_ma`suff2' mu_jt_m1_hat_preY_maN`suff2' mu_jt_m2_hat_preY_maN`suff2' mean_n_ma`suff2' va_ma_tot`suff2' va_ma_tot`suff2'_tbar commute_time_tbar commute_time_miss_tbar ///
 frac_black frac_hisp n_disadv mn_achievement mean_n_ma`suff2' blackXfrac_black hispanicXfrac_hisp VA_x_n_disadv`suff2' VA_x_frac_hisp`suff2' VA_x_frac_black`suff2' VA_x_mn_achievement`suff2' ///
 frac_black_tbar frac_hisp_tbar mn_achievement_tbar n_disadv_tbar blackXfrac_black_tbar hispanicXfrac_hisp_tbar VA_x_n_disadv`suff2'_tbar ///
 VA_x_frac_hisp`suff2'_tbar VA_x_frac_black`suff2'_tbar VA_x_mn_achievement`suff2'_tbar mean_n_ma`suff2'_tbar current_lea black hispanic female exp2 exp4 exp6 exp_missing current_school current_school_first ///
 mu_jt_m1_hat_preY_ma`suff2' mu_jt_m2_hat_preY_ma`suff2' teach_stayer teach_attriter
order itidx jtidx applied interview choiceset iidx jidx dist teachidx schoolidx app_year choicesetday1 yitidx yjtidx commute_time commute_time_miss ///
 hired offer_declined withdrew positive_assessment negative_assessment va_ma_preY`suff2' mu_jt_preY_mean_school_ma`suff2' mu_jt_m1_hat_preY_maN`suff2' mu_jt_m2_hat_preY_maN`suff2' mean_n_ma`suff2' va_ma_tot`suff2' va_ma_tot`suff2'_tbar commute_time_tbar commute_time_miss_tbar ///
 frac_black frac_hisp n_disadv mn_achievement mean_n_ma`suff2' blackXfrac_black hispanicXfrac_hisp VA_x_n_disadv`suff2' VA_x_frac_hisp`suff2' VA_x_frac_black`suff2' VA_x_mn_achievement`suff2' ///
 frac_black_tbar frac_hisp_tbar mn_achievement_tbar n_disadv_tbar blackXfrac_black_tbar hispanicXfrac_hisp_tbar VA_x_n_disadv`suff2'_tbar ///
 VA_x_frac_hisp`suff2'_tbar VA_x_frac_black`suff2'_tbar VA_x_mn_achievement`suff2'_tbar mean_n_ma`suff2'_tbar current_lea black hispanic female exp2 exp4 exp6 exp_missing current_school current_school_first ///
  mu_jt_m1_hat_preY_ma`suff2' mu_jt_m2_hat_preY_ma`suff2' teach_stayer teach_attriter

outsheet using "$basedata/estimationdata/All_T_J/All_T_J_choices_buff_0_0_lf_tbar_multiapp`suff2'.csv", comma replace
restore

local suff2 = ""
use "$basedata/estimationdata/All_T_J/teach_pref_est_data.dta", clear


keep if estsampall==1

replace choiceset = 0 if abs(first_person_app-PostingDate)<=7 | current_school==1

bys itidx: egen maxchoiceseti = max(choiceset)
bys jtidx: egen maxchoicesetj = max(choiceset)

drop if maxchoiceseti==0 | maxchoicesetj==0

sort itidx jtidx

set seed 332899
gen sortvar = runiform()
gsort itidx -current_school sortvar
gen current_school_first = current_school==1 & itidx[_n-1]!=itidx

sort itidx jtidx

foreach var in itidx jtidx iidx jidx {
    ren `var' `var'_orig
	egen `var' = group(`var'_orig)
	drop `var'_orig
}

preserve
keep itidx jtidx applied interview choiceset iidx jidx dist teachidx schoolidx app_year choicesetday1 yitidx yjtidx commute_time commute_time_miss ///
 hired offer_declined withdrew positive_assessment negative_assessment va_ma_preY`suff2' mu_jt_preY_mean_school_ma`suff2' mu_jt_m1_hat_preY_maN`suff2' mu_jt_m2_hat_preY_maN`suff2' mean_n_ma`suff2' va_ma_tot`suff2' va_ma_tot`suff2'_tbar commute_time_tbar commute_time_miss_tbar ///
 frac_black frac_hisp n_disadv mn_achievement mean_n_ma`suff2' blackXfrac_black hispanicXfrac_hisp VA_x_n_disadv`suff2' VA_x_frac_hisp`suff2' VA_x_frac_black`suff2' VA_x_mn_achievement`suff2' ///
 frac_black_tbar frac_hisp_tbar mn_achievement_tbar n_disadv_tbar blackXfrac_black_tbar hispanicXfrac_hisp_tbar VA_x_n_disadv`suff2'_tbar ///
 VA_x_frac_hisp`suff2'_tbar VA_x_frac_black`suff2'_tbar VA_x_mn_achievement`suff2'_tbar mean_n_ma`suff2'_tbar current_lea black hispanic female exp2 exp4 exp6 exp_missing current_school current_school_first ///
 mu_jt_m1_hat_preY_ma`suff2' mu_jt_m2_hat_preY_ma`suff2' teach_stayer teach_attriter
order itidx jtidx applied interview choiceset iidx jidx dist teachidx schoolidx app_year choicesetday1 yitidx yjtidx commute_time commute_time_miss ///
 hired offer_declined withdrew positive_assessment negative_assessment va_ma_preY`suff2' mu_jt_preY_mean_school_ma`suff2' mu_jt_m1_hat_preY_maN`suff2' mu_jt_m2_hat_preY_maN`suff2' mean_n_ma`suff2' va_ma_tot`suff2' va_ma_tot`suff2'_tbar commute_time_tbar commute_time_miss_tbar ///
 frac_black frac_hisp n_disadv mn_achievement mean_n_ma`suff2' blackXfrac_black hispanicXfrac_hisp VA_x_n_disadv`suff2' VA_x_frac_hisp`suff2' VA_x_frac_black`suff2' VA_x_mn_achievement`suff2' ///
 frac_black_tbar frac_hisp_tbar mn_achievement_tbar n_disadv_tbar blackXfrac_black_tbar hispanicXfrac_hisp_tbar VA_x_n_disadv`suff2'_tbar ///
 VA_x_frac_hisp`suff2'_tbar VA_x_frac_black`suff2'_tbar VA_x_mn_achievement`suff2'_tbar mean_n_ma`suff2'_tbar current_lea black hispanic female exp2 exp4 exp6 exp_missing current_school current_school_first ///
  mu_jt_m1_hat_preY_ma`suff2' mu_jt_m2_hat_preY_ma`suff2' teach_stayer teach_attriter

outsheet using "$basedata/estimationdata/All_T_J/All_T_J_choices_buff_0_0_lf_tbar_wkdonut`suff2'.csv", comma replace

restore

local suff2 = ""
use "$basedata/estimationdata/All_T_J/teach_pref_est_data.dta", clear


keep if estsampall==1

replace choiceset = choicesetday1

bys itidx: egen maxchoiceseti = max(choiceset)
bys jtidx: egen maxchoicesetj = max(choiceset)

drop if maxchoiceseti==0 | maxchoicesetj==0

sort itidx jtidx

set seed 332899
gen sortvar = runiform()
gsort itidx -current_school sortvar
gen current_school_first = current_school==1 & itidx[_n-1]!=itidx

sort itidx jtidx

foreach var in itidx jtidx iidx jidx {
    ren `var' `var'_orig
	egen `var' = group(`var'_orig)
	drop `var'_orig
}

preserve
keep itidx jtidx applied interview choiceset iidx jidx dist teachidx schoolidx app_year choicesetday1 yitidx yjtidx commute_time commute_time_miss ///
 hired offer_declined withdrew positive_assessment negative_assessment va_ma_preY`suff2' mu_jt_preY_mean_school_ma`suff2' mu_jt_m1_hat_preY_maN`suff2' mu_jt_m2_hat_preY_maN`suff2' mean_n_ma`suff2' va_ma_tot`suff2' va_ma_tot`suff2'_tbar commute_time_tbar commute_time_miss_tbar ///
 frac_black frac_hisp n_disadv mn_achievement mean_n_ma`suff2' blackXfrac_black hispanicXfrac_hisp VA_x_n_disadv`suff2' VA_x_frac_hisp`suff2' VA_x_frac_black`suff2' VA_x_mn_achievement`suff2' ///
 frac_black_tbar frac_hisp_tbar mn_achievement_tbar n_disadv_tbar blackXfrac_black_tbar hispanicXfrac_hisp_tbar VA_x_n_disadv`suff2'_tbar ///
 VA_x_frac_hisp`suff2'_tbar VA_x_frac_black`suff2'_tbar VA_x_mn_achievement`suff2'_tbar mean_n_ma`suff2'_tbar current_lea black hispanic female exp2 exp4 exp6 exp_missing current_school current_school_first ///
 mu_jt_m1_hat_preY_ma`suff2' mu_jt_m2_hat_preY_ma`suff2' teach_stayer teach_attriter
order itidx jtidx applied interview choiceset iidx jidx dist teachidx schoolidx app_year choicesetday1 yitidx yjtidx commute_time commute_time_miss ///
 hired offer_declined withdrew positive_assessment negative_assessment va_ma_preY`suff2' mu_jt_preY_mean_school_ma`suff2' mu_jt_m1_hat_preY_maN`suff2' mu_jt_m2_hat_preY_maN`suff2' mean_n_ma`suff2' va_ma_tot`suff2' va_ma_tot`suff2'_tbar commute_time_tbar commute_time_miss_tbar ///
 frac_black frac_hisp n_disadv mn_achievement mean_n_ma`suff2' blackXfrac_black hispanicXfrac_hisp VA_x_n_disadv`suff2' VA_x_frac_hisp`suff2' VA_x_frac_black`suff2' VA_x_mn_achievement`suff2' ///
 frac_black_tbar frac_hisp_tbar mn_achievement_tbar n_disadv_tbar blackXfrac_black_tbar hispanicXfrac_hisp_tbar VA_x_n_disadv`suff2'_tbar ///
 VA_x_frac_hisp`suff2'_tbar VA_x_frac_black`suff2'_tbar VA_x_mn_achievement`suff2'_tbar mean_n_ma`suff2'_tbar current_lea black hispanic female exp2 exp4 exp6 exp_missing current_school current_school_first ///
  mu_jt_m1_hat_preY_ma`suff2' mu_jt_m2_hat_preY_ma`suff2' teach_stayer teach_attriter

outsheet using "$basedata/estimationdata/All_T_J/All_T_J_choices_buff_0_0_lf_tbar_day1`suff2'.csv", comma replace

restore

local suff2 = ""
use "$basedata/estimationdata/All_T_J/teach_pref_est_data.dta", clear


keep if estsampall==1

replace choiceset = choiceset7

bys itidx: egen maxchoiceseti = max(choiceset)
bys jtidx: egen maxchoicesetj = max(choiceset)

drop if maxchoiceseti==0 | maxchoicesetj==0

sort itidx jtidx

set seed 332899
gen sortvar = runiform()
gsort itidx -current_school sortvar
gen current_school_first = current_school==1 & itidx[_n-1]!=itidx

sort itidx jtidx

foreach var in itidx jtidx iidx jidx {
    ren `var' `var'_orig
	egen `var' = group(`var'_orig)
	drop `var'_orig
}

preserve
keep itidx jtidx applied interview choiceset iidx jidx dist teachidx schoolidx app_year choicesetday1 yitidx yjtidx commute_time commute_time_miss ///
 hired offer_declined withdrew positive_assessment negative_assessment va_ma_preY`suff2' mu_jt_preY_mean_school_ma`suff2' mu_jt_m1_hat_preY_maN`suff2' mu_jt_m2_hat_preY_maN`suff2' mean_n_ma`suff2' va_ma_tot`suff2' va_ma_tot`suff2'_tbar7 commute_time_tbar7 commute_time_miss_tbar7 ///
 frac_black frac_hisp n_disadv mn_achievement mean_n_ma`suff2' blackXfrac_black hispanicXfrac_hisp VA_x_n_disadv`suff2' VA_x_frac_hisp`suff2' VA_x_frac_black`suff2' VA_x_mn_achievement`suff2' ///
 frac_black_tbar7 frac_hisp_tbar7 mn_achievement_tbar7 n_disadv_tbar7 blackXfrac_black_tbar7 hispanicXfrac_hisp_tbar7 VA_x_n_disadv`suff2'_tbar7 ///
 VA_x_frac_hisp`suff2'_tbar7 VA_x_frac_black`suff2'_tbar7 VA_x_mn_achievement`suff2'_tbar7 mean_n_ma`suff2'_tbar7 current_lea black hispanic female exp2 exp4 exp6 exp_missing current_school current_school_first ///
 mu_jt_m1_hat_preY_ma`suff2' mu_jt_m2_hat_preY_ma`suff2' teach_stayer teach_attriter
order itidx jtidx applied interview choiceset iidx jidx dist teachidx schoolidx app_year choicesetday1 yitidx yjtidx commute_time commute_time_miss ///
 hired offer_declined withdrew positive_assessment negative_assessment va_ma_preY`suff2' mu_jt_preY_mean_school_ma`suff2' mu_jt_m1_hat_preY_maN`suff2' mu_jt_m2_hat_preY_maN`suff2' mean_n_ma`suff2' va_ma_tot`suff2' va_ma_tot`suff2'_tbar7 commute_time_tbar7 commute_time_miss_tbar7 ///
 frac_black frac_hisp n_disadv mn_achievement mean_n_ma`suff2' blackXfrac_black hispanicXfrac_hisp VA_x_n_disadv`suff2' VA_x_frac_hisp`suff2' VA_x_frac_black`suff2' VA_x_mn_achievement`suff2' ///
 frac_black_tbar7 frac_hisp_tbar7 mn_achievement_tbar7 n_disadv_tbar7 blackXfrac_black_tbar7 hispanicXfrac_hisp_tbar7 VA_x_n_disadv`suff2'_tbar7 ///
 VA_x_frac_hisp`suff2'_tbar7 VA_x_frac_black`suff2'_tbar7 VA_x_mn_achievement`suff2'_tbar7 mean_n_ma`suff2'_tbar7 current_lea black hispanic female exp2 exp4 exp6 exp_missing current_school current_school_first ///
 mu_jt_m1_hat_preY_ma`suff2' mu_jt_m2_hat_preY_ma`suff2' teach_stayer teach_attriter

outsheet using "$basedata/estimationdata/All_T_J/All_T_J_choices_buff_0_7_lf_tbar_`suff2'.csv", comma replace

restore



use "$basedata/estimationdata/All_T_J/teach_pref_est_data.dta", clear

keep if estsampall==1

set seed 332899
gen sortvar = runiform()
gsort itidx -current_school sortvar
gen current_school_first = current_school==1 & itidx[_n-1]!=itidx

sort itidx jtidx

gen month_first_app = month(first_person_app)
gen month_last_app = month(last_person_app)

gen posting_year = year(PostingDate)
gen posting_month = month(PostingDate)


keep itidx jtidx app_year month_first_app month_last_app posting_year posting_month titleI
order itidx jtidx app_year month_first_app month_last_app posting_year posting_month titleI

outsheet using "$basedata/estimationdata/All_T_J/All_T_J_choices_buff_0_0_lf_tbar_totVA_month.csv", comma replace

